<template>
  <el-table :data="user_data" border stripe style="width: 100%">
    <el-table-column type="index"></el-table-column>
    </el-table-column>
    <el-table-column prop="ID" label="ID">
    </el-table-column>
    <el-table-column prop="Username" label="姓名">
    </el-table-column>
    <el-table-column prop="Class" label="权限">
    </el-table-column>
    </el-table-column>
    <el-table-column label="用户状态">
      <template slot-scope="scope">
        <el-switch v-model="scope.row.Status" active-color="#13ce66" inactive-color="#ff4949"
          @change="userStateChange(scope.row)" :disabled="status">
        </el-switch>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
  export default{
    data() {
      return {
        user_data: [],
        status: false,
      }
    },
    created() {
      this.getMenuList();
      this.Disable();
    },
    methods: {
      //获取用户数据
      async getMenuList() {
        const result = await this.axios.get("/api/user_list");
        if (result.data.status === 200) {
          this.user_data = result.data.data;
          this.$store.commit("get_Urer_Info",this.user_data);
          console.log(this.user_data);
        } else {
          this.$message.error("数据请求失败")
        }
      },
      //判断权限
      Disable() {
        this.status = false;
        if (sessionStorage.getItem("Class") !== "root") {
          this.status = true;
        }
        return status;
      },
      //发送修改状态的代码
      async userStateChange(userinfo) {
        console.log(userinfo);
        let obj = {
          uid: userinfo.ID,
          Status: userinfo.Status
        }
        const result = await this.axios.put("/api/user_status",JSON.stringify(obj));
        if (result.data.status === 200) {
          this.$message.success("更新状态成功");
          this.$store.commit("get_Urer_Info",this.user_data);
        } else {
          userinfo.user_status = !userinfo.user_status;
          this.$message.error("修改失败，请稍后再试!");
        }
      },
    },
    watch:{
      user_data(){
          this.$store.commit("get_Urer_Info",this.user_data);
      }
    }
  }
</script>

<style>
</style>
